package com.jzoom.zoom.admin.controllers;

import com.jzoom.zoom.admin.entities.DecoTableVo;
import com.jzoom.zoom.admin.models.DecorationTableService;
import com.jzoom.zoom.common.annotations.Inject;
import com.jzoom.zoom.dao.Record;
import com.jzoom.zoom.web.annotations.Controller;
import com.jzoom.zoom.web.annotations.JsonResponse;
import com.jzoom.zoom.web.annotations.Mapping;
import com.jzoom.zoom.web.annotations.Param;
import com.jzoom.zoom.web.exception.StatusException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

@Controller(key = "sys/sys_deco_table")
public class DecoTableController implements AdminController {

	@Inject
	private DecorationTableService tableDao;

	@JsonResponse
	@Mapping(value = "list", method = { Mapping.POST })
	public List<Record> list(@Param(name = "@") Map<String, Object> params) {
		return tableDao.getDecorated(params);
	}


	// 查询所有的表和所有的deco整合起来
	@JsonResponse
	@Mapping(value = "get/{table}", method = Mapping.POST)
	public DecoTableVo get(@Param(name = "{table}") String table) {
		if (table == null) {
			throw new StatusException.ApiError("参数错误");
		}

		if (table.contains("@")) {
			String[] parts = table.split("@");
			String tables = parts[1];
			if (StringUtils.isEmpty(tables)) {
				throw new StatusException.ApiError("表名称为空");
			}
			String[] tableAndLinks = tables.split(",");
			return tableDao.getTable(parts[0], tableAndLinks);
		} else {
			return tableDao.getTable(table);
		}
	}

	@JsonResponse
	@Mapping(value = "add", method = { Mapping.POST })
	public Object add(Boolean enableFilter, String tmpId, String ds, String name, String comment,
                      List<Map<String, Object>> columns, String select,
                      List<Map<String, Object>> filterColumns,
                      List<Map<String, Object>> links,
                      Map<String, Object> props) {

        return tableDao.add(enableFilter, tmpId, ds, name, comment, columns, select, links,
                filterColumns, props);

	}

	@JsonResponse
	@Mapping(value = "put/{id}", method = Mapping.POST)
	public int put(@Param(name = "{id}") String id, Boolean enableFilter, String tmpId, String ds, String name,
                   String comment, List<Map<String, Object>> columns, String select,
                   List<Map<String, Object>> filterColumns,
                   List<Map<String, Object>> links, Map<String, Object> props) {

        return tableDao.update(id, enableFilter, tmpId, ds, name, comment, columns, select, links, filterColumns, props);
	}
}
